package t327.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import t327.pojo.PayOrderTable;
import t327.pojo.Role;
import t327.pojo.ali.PayCall;
import t327.pojo.font.LuiTableEntity;
import t327.pojo.views.ContractOrderPayView;
import t327.service.iter.IContractOrderPayService;
import t327.util.AlipayUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("pay")
public class PayContractController {

        @Autowired
        private IContractOrderPayService contractOrderPayService;
        @RequestMapping("payContractOrder")
        public LuiTableEntity quertyContractOrderClient(@RequestParam(value = "page") Integer pageNum, @RequestParam(value = "limit")Integer pageSize){
            PageHelper.startPage(pageNum,pageSize);
            LuiTableEntity luiTableEntity = new LuiTableEntity<ContractOrderPayView>();
            PageInfo<ContractOrderPayView> contractOrderPayViewPageInfo = new PageInfo<>(contractOrderPayService.queryPay());
            List<ContractOrderPayView> contractOrderPayViews = contractOrderPayViewPageInfo.getList();
            long total = contractOrderPayViewPageInfo.getTotal();
            if(contractOrderPayViews!=null&&contractOrderPayViews.size()>0){
                luiTableEntity.setCode("0");
                luiTableEntity.setMsg("查询成功");
                luiTableEntity.setCount(total);
                luiTableEntity.setData(contractOrderPayViews);
            }else{
                luiTableEntity.setCode("202");
                luiTableEntity.setMsg("查询失败");
            }
            return   luiTableEntity;
        }
        @RequestMapping("payForPayContract")
        public HashMap payForPayCode(@RequestBody HashMap<String,String> map, HttpSession session){
            HashMap<String, String> hashMap = new HashMap<>();
            String contractNumber = map.get("contractNumber");
            String payCode = map.get("payCode");

            String clientId = map.get("clientId");
            Role role= (Role) session.getAttribute("role1");
            if(role==null||role.getRoleId()==null){
                hashMap.put("code","205" );
                hashMap.put("msg","请登录再进行缴费操作" );
                return hashMap;
            }
            return  contractOrderPayService.payForPayCode(contractNumber,payCode,role.getRoleId(),Long.parseLong(clientId));

        }
        @RequestMapping("refundForOrder")
        public HashMap refundForOrder(@RequestBody HashMap<String,String> map, HttpSession session){
            HashMap<String, String> hashMap = new HashMap<>();
            String outTradeNo = map.get("outTradeNo");

            return contractOrderPayService.refundForOrder(outTradeNo);

        }
        @RequestMapping("getQrCode")
        public Map<String,String> getQrCode(@RequestBody PayOrderTable payOrderTable, HttpSession session){
            Role role= (Role) session.getAttribute("role1");
            Long roleId=role.getRoleId();
            return contractOrderPayService.preCreate(payOrderTable.getContractNumber(), payOrderTable.getClientId(),roleId);
        }
        //跟支付宝交接得出支付宝是否收到钱
        @RequestMapping("confirmPay")
        public String pay(PayCall payCall, HttpServletRequest request) {
            String outTradeNo = payCall.getOut_trade_no();
            Map<String, String> parameterMap= AlipayUtil.convertRequestParamsToMap(request);
            HashMap<String,String> map=contractOrderPayService.confirmPay(outTradeNo,parameterMap);
            String code = map.get("code");
            if(code!=null&&code.equals("200")){
                return "success";
            }
            return "false";
        }
        @RequestMapping("confirmFee")
        public Map confirmFee(String contractNumber) {
            HashMap<String, String> map = new HashMap<>();
            PayOrderTable payOrderTable = contractOrderPayService.findOrderById(contractNumber);
            if(payOrderTable!=null){
                boolean isOk = payOrderTable.getPayType()==2L;
                if(isOk){
                    map.put("code","200");
                    map.put("msg","付款成功");
                }else{
                    map.put("code","201");
                    map.put("msg","订单状态不是已付款");
                }
            }else{
                map.put("code","202");
                map.put("msg","没有该订单");
            }
            return map;
        }
}
